import string
import random
import os

from django.http import QueryDict

from utils.resFormatUyil import FormatResponse
from PIL import Image,ImageFilter,ImageFont,ImageDraw

# 该装饰器功能，用来检测使用该装饰器的函数是否传入了参数
def verfy_params(*params):
    def verify_func(func):
        def is_none(request,*args,**kwargs):
            try:
                request_param = getattr(request,request.method)
            except:
                request_param = QueryDict(request.body)
            for param in params:
                value = request_param.get(param)
                if not value:
                    return FormatResponse(status='PARAM_ERROR').res()
            return func(request,*args,**kwargs)
        return is_none
    return verify_func

# 实现生成验证码图片的功能
class Image_verify:
    def __init__(self,length=4,width=280,height=70,size=38):
        '''

        :param length: 图片验证码的字符长度
        :param width: 图片的宽度
        :param height: 图片的高度
        :param size: 验证码字体的大小
        '''
        self.length = length
        self.width = width
        self.height = height
        self.size = size

    # 生成随机验证码
    def random_code(self):
        # 生成随机验证码的范围
        s = string.digits + string.ascii_letters
        # 生成随机验证码
        code = random.sample(s,self.length)
        return ''.join(code)

    # 生成随机颜色
    def random_color(self,start,end):
        color = (random.randint(start,end) for i in range(3))
        return tuple(color)

    # 生成随机干扰线
    def random_lines(self,draw):
        for i in range(self.length):
            line = ((random.randint(0,self.width),random.randint(0,self.height)) for i in range(2))
            draw.line(xy=list(line),fill=self.random_color(64,255),width=3)

    # 生成随机干扰点
    def random_points(self,draw,rate):
        for x in range(self.width):
            for y in range(self.height):
                if random.randint(0,100) <= rate:
                    draw.point(xy=(x,y),fill=self.random_color(64,255))


    def image_code(self):
        # 生成空白背景图片
        img = Image.new(mode='RGB',size=(self.width,self.height),color=(255,255,255))
        # 生成写入对象
        draw = ImageDraw.Draw(im=img)
        # 获取随机验证码
        code = self.random_code()
        # 获取字体格式
        font_path = os.path.dirname(os.path.abspath(__file__))
        font_path = font_path + '/HYYakuHei-85W.ttf'
        font = ImageFont.truetype(font_path,size=self.size)
        # 将验证码写入到背景图片当中
        for i in range(len(code)):
            draw.text(xy=(i*50+60,10),text=code[i],fill=self.random_color(0,64),font=font)
        # 生成干扰线
        self.random_lines(draw=draw)
        # 生成随机干扰点
        self.random_points(draw=draw,rate=18)
        return code,img

# if __name__ == '__main__':
#     img = Image_verify().image_code()
#     with open('test.png','wb') as f:
#         img.save(f)





















# import random
# import string
# import os
#
# from django.http import HttpResponse
#
# from utils.resFormatUyil import ResFormat
# from PIL import ImageFilter,ImageFont,ImageDraw,Image
#
# def verify_param(*param):
#     def verify(func):
#         def is_none(request,*args,**kwargs):
#             request_method = getattr(request,request.method)
#             for i in param:
#                 value = request_method.get(i)
#                 if not value:
#                     return ResFormat(status='PARAM_ERROR').json_response()
#             return func(request,*args,**kwargs)
#         return is_none
#     return verify
#
# class ImageVerify:
#     def __init__(self,length=4,width=120,height=30,size=20):
#         '''
#
#         :param length: 字符长度
#         :param width: 图片宽度
#         :param height: 图片高度
#         '''
#         self.length = length
#         self.width = width
#         self.height = height
#         self.size = size
#
#     # 生成随机验证码
#     def random_code(self):
#         # 获取资源对象区域
#         source = string.digits + string.ascii_letters
#         # 生成随机验证码
#         code = ''.join(random.sample(source,self.length))
#         return code
#
#     # 生成随机颜色
#     def random_color(self,start,end):
#         color = (random.randint(start,end) for i in range(3))
#         return tuple(color)
#
#     # 生成随机干扰线
#     def random_lines(self,draw):
#         for i in range(self.length):
#             lines = [(random.randint(0,self.width),random.randint(0,self.height)) for i in range(2)]
#             draw.line(list(lines),fill=self.random_color(64,255))
#
#     # 生成随机干扰点
#     def random_points(self,draw,rate):
#         for x in range(self.width):
#             for y in range(self.height):
#                 if random.randint(0,100) <= rate:
#                     draw.point(xy=(x,y),fill=self.random_color(64,255))
#
#     def image_verify(self):
#         # 生成白色背景图片
#         image = Image.new(mode='RGB',size=(self.width,self.height),color=(255,255,255))
#         # 获取需要画入内容的图片对象
#         draw = ImageDraw.Draw(image)
#         # 获取随机验证码
#         code = self.random_code()
#         # 获取字体格式
#         file = os.path.dirname(os.path.abspath(__file__))
#         font = ImageFont.truetype(font=f'{file}/HYYakuHei-85W.ttf',size=self.size)
#         # 将随机验证码画入到背景图上
#         for i in range(self.length):
#             draw.text(xy=(30*i+5,0),text=code[i],fill=self.random_color(0,64),font=font)
#         # 绘制干扰线
#         self.random_lines(draw=draw)
#         # 绘制干扰点
#         self.random_points(draw=draw,rate=18)
#         # image = image.filter(ImageFilter.BLUR)
#         return image,code
#
#
# # if __name__ == '__main__':
# #     with open('test.png','wb') as f:
# #         image = ImageVerify().image_verify()
# #         image.save(f)
#
